package com.tyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyg.entity.UserInfo;
import com.tyg.mapper.UserInfoMapper;
import com.tyg.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工作人员表 服务实现类
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@Service
@Transactional
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    UserInfoMapper userMapper;

    @Override
    public boolean isIdCard(String workIdCard) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<UserInfo>();
        queryWrapper.eq("workIdCard", workIdCard);//判断该角色存不存在
        UserInfo user=userMapper.selectOne(queryWrapper);
        if(user == null) {//不存在
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean addNewUser(UserInfo user) {
        String IdCard=user.getWorkIdCard();
        boolean resultAdd=isIdCard(IdCard);
        if(resultAdd){
            user.setRoleId(9);
            userMapper.insert(user);
            resultAdd = true;
        }else{
            resultAdd = false;
        }
        return resultAdd;
    }

    @Override
    public UserInfo selectOne(QueryWrapper<UserInfo> queryWrapper) {
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserInfo> getUserList(Map<String, Object> map) {
        return userMapper.getUserList(map);
    }

    @Override
    public int insert(UserInfo user) {
        return userMapper.insert(user);
    }

    @Override
    public UserInfo selectById(int workId) {
        return userMapper.selectById(workId);
    }

    @Override
    public boolean updateById(UserInfo user) {
        int updUser=userMapper.updateById(user);
        if(updUser==0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public void updateUserRole(UserInfo user) {
        userMapper.updateUserRole(user);
    }

    @Override
    public List<UserInfo> selectList() {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("roleId",6);
        return userMapper.selectList(queryWrapper);
    }


}
